package com.sgb.kjwl.view.ui.activity.pick;


import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPickTenderInquiryBinding;
import com.sgb.kjwl.model.entity.pick.TenderInquiryType;
import com.sgb.kjwl.view.ui.adapter.pick.holder.PickDetailInquiryAdapter;
import com.sgb.kjwl.viewmodel.pick.PickTenderInquiryModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 招标详情-函询列表
 */
public class PickTenderInquiryActivity extends AppActivity {

    private PickDetailInquiryAdapter pickDetailInquiryAdapter;
    private ActivityPickTenderInquiryBinding binding;
    private PickTenderInquiryModel sModel;
    private String titleName;
    private String tenderId;
    private boolean isMore = false;

    public static void start(Context context, String tenderId, String titleName) {
        Intent intent = new Intent(context, PickTenderInquiryActivity.class);
        intent.putExtra("tenderId", tenderId);
        intent.putExtra("titleName", titleName);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        titleName = getIntent().getStringExtra("titleName");
        tenderId = getIntent().getStringExtra("tenderId");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_tender_inquiry);
        binding.setLifecycleOwner(this);
        sModel = new ViewModelProvider(this).get(PickTenderInquiryModel.class);
        sModel.findTenderInquiryPage(tenderId);
        registerObserver();
        initData();
    }

    private void registerObserver() {
        sModel.dialog.observe(this, aBoolean -> {
            if (aBoolean) {
                if (!DialogHelper.isShowing()) {
                    DialogHelper.showProgressDialog(PickTenderInquiryActivity.this, null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                }
            } else {
                DialogHelper.dismissProgressDialog();
            }
        });
        sModel.tenderInquiryList.observe(this, new Observer<List<TenderInquiryType>>() {
            @Override
            public void onChanged(List<TenderInquiryType> lists) {
                binding.recyclerView.refreshComplete();
                if (isMore) {
                    binding.recyclerView.loadMoreComplete();
                    if (lists.size() > 0) {
                        pickDetailInquiryAdapter.addDatas(lists);
                    } else {
                        binding.recyclerView.setLoadingMoreEnabled(false);
                    }
                } else {
                    binding.recyclerView.refreshComplete();
                    if (lists.size() > 0) {
                        binding.empty.setVisibility(View.GONE);
                        binding.recyclerView.setVisibility(View.VISIBLE);
                        pickDetailInquiryAdapter.setDatas(lists);
                        binding.recyclerView.setLoadingMoreEnabled(true);
                    } else {
                        pickDetailInquiryAdapter.setDatas(new ArrayList<>());
                        binding.recyclerView.setVisibility(View.GONE);
                        binding.empty.setVisibility(View.VISIBLE);
                    }
                }
                pickDetailInquiryAdapter.notifyDataSetChanged();
            }
        });
        sModel.showToast.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(getActivity(), s);
            }
        });
    }

    private void initData() {
        binding.tvTitle.setText(titleName + "-补遗");
        binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        pickDetailInquiryAdapter = new PickDetailInquiryAdapter(getActivity(), null);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divide3));
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setAdapter(pickDetailInquiryAdapter);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isMore = false;
                //招标中
                sModel.page.set(1);
                sModel.findTenderInquiryPage(tenderId);
            }

            @Override
            public void onLoadMore() {
                isMore = true;
                sModel.page.set(sModel.page.get() + 1);
                sModel.findTenderInquiryPage(tenderId);
            }
        });

        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.imgBackTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.recyclerView.getLayoutManager().scrollToPosition(0);
            }
        });
    }
}